import com.alibaba.alimonitor.jmonitor.utils.StringUtils
import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import redis.clients.jedis.Jedis
import script.GlobalValues
import script.moco.dto.FeeRefundRequest
import script.moco.dto.FeeRefundResponse
import script.moco.dto.OrderInfo
import script.moco.dto.OrderPriceDetail
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.NotifyRequest
//客服退款
def excute(data) {

    Jedis redis = RedisPoolInstance.getRedisClient();
    def params = data.getParameterMap();
    def request = BaseTypeUtil.mapToBean(params, FeeRefundRequest.class) as FeeRefundRequest;//请求数据转换为对象

    def response = new FeeRefundResponse(); //定义响应对象

    def jsonStr = redis.get(GlobalValues.KEY_ORDER + request.getOrderId());//从 Redis中获取 json 订单字符串
    if (jsonStr == null) {    //验证订单是否存在
        response.setSuccess("false");
        response.setErrorMsg("订单id不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def slurper = new JsonSlurper();
    def detailMap = slurper.parseText(jsonStr);
    if (!detailMap.containsKey("order")) {
        response.setSuccess("false");
        response.setErrorMsg("订单不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def jsonMap = detailMap.get("order");//从json字符串变成json对象

    def taxiObj = BaseTypeUtil.jsonToBean(jsonMap, OrderInfo.class) as OrderInfo;//将json对象转换为TaxiRequest对象

    System.out.println(detailMap.price.total_price);
    if (request.getAmount() > Double.valueOf(detailMap.price.total_price)) {
        response.setSuccess("false");
        response.setErrorMsg("参数错误 !");
        response.setErrorCode(10003);
        response.setErrorLevel(1);
        response.setContent("退款金额超过订单总价");

        return response;
    }
    //客服退款完善数据
    System.out.println(detailMap.price.total_price)
    detailMap.price.total_price = detailMap.price.total_price - request.getAmount();

    if (detailMap.containsKey("price")) {
        OrderPriceDetail det = new OrderPriceDetail();
        System.out.println("设置费用详情")
        det.setAmount(String.valueOf(request.getAmount()))
        det.setName("退款金额")
        det.setType("refund_price")
        System.out.println(detailMap.price.detail)

        detailMap.price.detail.add(det);
    }
    //taxiObj.setPayAmount(detailMap.price.total_price-request.getAmount());

    detailMap.put("order", taxiObj)
    //detailMap.put("price", detailPrice)

    redis.set(GlobalValues.KEY_ORDER + taxiObj.getId(), JsonOutput.toJson(detailMap).toString())    //直接覆盖原有数据，进行修改

    //客服退款完成后进行回调
    //通知回调
    NoticeCallback noticeCallbackTool = new NoticeCallback();
    NotifyRequest notifyRequest = new NotifyRequest();
    notifyRequest.setOrderId(taxiObj.getId());
    notifyRequest.setNotifyType(4);
    notifyRequest.setNotifyDesc("订单退款通知：产生投诉的订单，退款操作后，通知接入方调用订单详情获取退款信息");
    String url = noticeCallbackTool.getCallbackUrlFromRequest(data)
    if (StringUtils.isNotBlank(url)){
        response.setSuccess("true");
        response.setErrorMsg(null);
        response.setErrorCode(0);
        response.setErrorLevel(0);
        response.setContent(noticeCallbackTool.handNotify(url, notifyRequest));
    } else {
        response.setSuccess("false");
        response.setErrorMsg("session 中不存在callback地址");
        response.setErrorCode(-1);
        response.setErrorLevel(0);
    }
    /*def callbackObj = new CallbackRequest();
    callbackObj.setClientId("1");
    callbackObj.setOrderId(taxiObj.getId());
    callbackObj.setNotifyType(4);
    callbackObj.setNotifyDesc("订单退款通知：产生投诉的订单，退款操作后，通知接入方调用订单详情获取退款信息");
    callbackObj.setTimestamp(System.currentTimeMillis());
    callbackObj.setSign("123");

    HttpClientUtil.post(callbackObj);

    response.setSuccess("true");
    response.setErrorMsg(null);
    response.setErrorCode(0);
    response.setErrorLevel(0);
    response.setContent(null);*/

    return response;
}